<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Peserta_ujian extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_peserta_ujian');
		$this->load->model('mdl_mahasiswa');
		
		// $this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$this->load->view('perkuliahan/peserta_ujian');
	}
	
	# grid
	function grid(){
		$data = $this->mdl_peserta_ujian->getdata();
		echo $this->mdl_peserta_ujian->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_detail($id){
		$data = $this->mdl_peserta_ujian->getdata_detail($id);
		echo $this->mdl_peserta_ujian->togrid($data['row_data'], $data['row_count']);
	}
	# end grid
	
	function getdata_matkul(){
		$kd_kurikulum = $this->input->post('kd_kurikulum');
		$kd_prodi = $this->input->post('kd_prodi');
		$id_tahunajaran = $this->input->post('id_tahunajaran');
		if($kd_kurikulum != ''){
			$data = $this->mdl_peserta_ujian->getmatakuliah($kd_kurikulum, $kd_prodi, $id_tahunajaran);
			echo $this->mdl_peserta_ujian->togrid($data['row_data'], $data['row_count']);	
		}else{echo json_encode(array('total'=>0, 'rows'=>array('kd_matakuliah'=>'')));}
	}
	
	function getsemester_mhs($nim){
		$res = $this->mdl_peserta_ujian->getsemester_mhs($nim);
		echo json_encode(array('semester'=>$res));
	}
	
	function getdata_mhs(){
		$mhs = $this->mdl_mahasiswa->getdataByNIM($this->input->post('nim'));
		
		if($mhs->num_rows()>0){
			$data['nama'] = $mhs->row()->nama;
			$data['nama_prodi'] = $mhs->row()->nama_prodi;
			echo json_encode($data);
		}else{
			echo json_encode(array('error'=>true));
		}
	}

	function load_id_jadwal($id_tahun_ajaran, $kd_prodi, $kd_matakuliah){
		$data = $this->mdl_peserta_ujian->load_id_jadwal($id_tahun_ajaran, $kd_prodi, $kd_matakuliah);
		//$arr = array('id_jadwal'=>'0', 'nama_dosen'=>'');
		$arr = array();
		array_push($arr, array('id_jadwal'=>'', 'nama_dosen'=>'&nbsp;'));
		foreach($data['row_data']->result() as $row){
			array_push($arr, array('id_jadwal'=>$row->id_jadwal, 'nama_dosen'=>$row->nama_dosen));
		}
		echo json_encode($arr);
	}
	
	function laporan_pdf_uts($kd_fakultas,$kd_prodi,$tahun,$kd_matakuliah,$kd_dosen){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_fakultas'] = $kd_fakultas;
		$fil['kd_prodi'] = $kd_prodi;
		$fil['tahun'] = $tahun;
		$fil['kd_matakuliah'] = $kd_matakuliah;
		$fil['kd_dosen'] = $kd_dosen;
		
		  //$data['nama'] = '';
		 $data['namaUniv'] = 'STMIK BANDUNG';
		 $data['alamatUniv'] = 'Jl.Phh.Mustofa No. 39. Grand Surapati Core (SUCORE) Blok M No.19, Telp.022 - 7207777';
		 $data['kotaUniv'] = 'Bandung, Jawa Barat';
		 // ambil data dari tabel
		 $data['data_pdf'] = $this->mdl_peserta_ujian->get_pdf_uts($fil,$kd_matakuliah);
		 
		 $konten = $this->load->view('perkuliahan/peserta_uts_laporan',$data,true);
		 
		 // echo $konten;
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('Peserta UTS.pdf');
	}
	
	function laporan_pdf_uas($kd_fakultas,$kd_prodi,$tahun,$kd_matakuliah,$kd_dosen){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_fakultas'] = $kd_fakultas;
		$fil['kd_prodi'] = $kd_prodi;
		$fil['tahun'] = $tahun;
		$fil['kd_matakuliah'] = $kd_matakuliah;
		$fil['kd_dosen'] = $kd_dosen;
		
		  //$data['nama'] = '';
		 $data['namaUniv'] = 'STMIK BANDUNG';
		 $data['alamatUniv'] = 'Jl.Phh.Mustofa No. 39. Grand Surapati Core (SUCORE) Blok M No.19, Telp.022 - 7207777';
		 $data['kotaUniv'] = 'Bandung, Jawa Barat';
		 // ambil data dari tabel
		 $data['data_pdf'] = $this->mdl_peserta_ujian->get_pdf_uts($fil,$kd_matakuliah);
		 
		 $konten = $this->load->view('perkuliahan/peserta_uas_laporan',$data,true);
		 
		 // echo $konten;
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('Peserta UAS.pdf');
	}
	
	function exportDaftarNilai($idThn,$kdMatkul, $kdDosen)
	{
		$this->load->library('Excel_generator', '', 'excelGen');

		$this->excelGen->getProperties()->setCreator("STMIK Bandung")->setTitle("DATA NILAI AKHIR MAHASISWA");
        
        $this->excelGen->setActiveSheetIndex(0); //inisiasi set object
        $objget = $this->excelGen->getActiveSheet();  //inisiasi get object

        $objget->setTitle('Data Nilai'); //sheet title

        $col = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M');
        $width = array(10, 10, 2, 18, 10, 10, 10, 10, 10, 12, 2, 10, 15);

        for($x = 0; $x < sizeof($col); $x++) {
        	$objget->getColumnDimension($col[$x])->setWidth($width[$x]);
        }

    	$objget->getRowDimension(11)->setRowHeight(55);


        $xter = array(10, 11);
        for($x = 0; $x < sizeof($col); $x++) {
   			$objget->getStyle($col[$x].$xter[0])->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
   			$objget->getStyle($col[$x].$xter[1])->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
   			$objget->getStyle($col[$x].$xter[0])->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
   			$objget->getStyle($col[$x].$xter[1])->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
   			$objget->getStyle($col[$x].$xter[0])->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
   			$objget->getStyle($col[$x].$xter[1])->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$objget->getStyle($col[$x].'8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_DOUBLE);
			$objget->getStyle($col[$x].'1')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
			$objget->getStyle($col[$x].'10')->getAlignment()->setWrapText(true);
			$objget->getStyle($col[$x].'11')->getAlignment()->setWrapText(true);
        }

    	$objget->setCellValue('A10', 'NO');
    	$objget->setCellValue('B10', 'NIM');
    	$objget->setCellValue('D10', 'Nama');
    	$objget->setCellValue('E10', 'Tanda Tangan');
    	$objget->setCellValue('F10', 'Komponen Nilai');
    	$objget->setCellValue('L10', 'Nilai Akhir');
    	$objget->setCellValue('F11', 'Nilai UTS');
    	$objget->setCellValue('G11', 'Nilai UAS');
    	$objget->setCellValue('H11', 'Nilai Tugas');
    	$objget->setCellValue('I11', 'Nilai Quiz');
    	$objget->setCellValue('J11', 'Kehadiran');
    	$objget->setCellValue('L11', 'Nilai Angka Mutu');
    	$objget->setCellValue('M11', 'Nilai Huruf Mutu');
    	$objget->mergeCells('A10:A11');
    	$objget->mergeCells('B10:C11');
    	$objget->mergeCells('D10:D11');
    	$objget->mergeCells('E10:E11');
    	$objget->mergeCells('F10:K10');
    	$objget->mergeCells('L10:M10');
    	$objget->mergeCells('J11:K11');

    	$query = "SELECT 
	a.nim, a.semester, a.id_tahunajaran, d.Nama as 'namaThn',
	b.nama, a.id_krs, a.kd_prodi, c.nama_prodi, f.nama as 'namaMtk', f.kd_matakuliah,
	f.id_jadwal, f.kd_kelas as kelas, g.kd_dosen, g.nama_dosen
FROM
	tbl_krs a
JOIN tbl_mahasiswa b ON b.nim = a.nim
JOIN ref_prodi c ON c.kd_prodi = a.kd_prodi
JOIN tbl_tahunajaran d ON d.id_tahunajaran = a.id_tahunajaran
JOIN tbl_krs_detail e ON e.id_krs = a.id_krs
JOIN tbl_jadwal f ON f.id_jadwal = e.id_jadwal
JOIN ref_dosen g ON g.kd_dosen = f.kd_dosen
JOIN ref_matakuliah h ON h.kd_matakuliah = f.kd_matakuliah

WHERE `a`.`id_tahunajaran` = '{$idThn}'
AND `h`.`kd_matakuliah` = '{$kdMatkul}'
AND `g`.`kd_dosen` = '{$kdDosen}'

GROUP BY a.nim, e.id_jadwal";

        $dbObj = $this->db->query($query)->result();

        $current = 12;
        $numbr = 1;
        foreach($dbObj as $obj) {
        	$objget->setCellValue('A'.$current, $numbr);
        	$objget->setCellValue('B'.$current, $obj->nim);
        	$objget->setCellValue('D'.$current, $obj->nama);
        	foreach ($col as $val) {
   				$objget->getStyle($val.$current)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        	}
        	$objget->mergeCells('B'.$current.':C'.$current);
        	$objget->mergeCells('J'.$current.':K'.$current);
        	if($numbr === 1) {
		        $objget->setCellValue('D3', $obj->semester);
		        $objget->setCellValue('D4', $obj->nama_prodi.' ('.$obj->kd_prodi.')');
		        $objget->setCellValue('D6', $obj->namaMtk.' ('.$obj->kd_matakuliah.')');
		        $objget->setCellValue('D7', $obj->nama_dosen.' ('.$obj->kd_dosen.')');
		        $objget->setCellValue('L3', $obj->namaThn.' ('.$obj->id_tahunajaran.')');
		        $objget->setCellValue('L6', $obj->kelas.' ('.$obj->id_jadwal.')');
        	}
        	$current++;
        	$numbr++;
        }

        // Title Bar
        $objget->setCellValue('A1',"DAFTAR NILAI AKHIR MAHASISWA"); //insert cell value
        $objget->getStyle('A1')->getFont()->setBold(true)  // set font weight
                ->setSize(15);    //set font size
        $objget->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objget->mergeCells('A1:M1');

        // Properties Title
        $objget->setCellValue('A3', 'SEMESTER');
        $objget->setCellValue('A4', 'PROGRAM STUDI');
        $objget->setCellValue('A6', 'MATA KULIAH');
        $objget->setCellValue('A7', 'DOSEN');

        $objget->setCellValue('I3', 'TAHUN AKADEMIK');
        $objget->setCellValue('I4', 'PROGRAM PENDIDIKAN');
        $objget->setCellValue('L4', 'SARJANA (S1)');
        $objget->setCellValue('I6', 'KODE JADWAL');

        for($i = 3; $i < 8; $i++) {
        	if($i == 5) { continue; }
        	$objget->setCellValue('C'.$i, ':');
        	if($i != 7) {$objget->setCellValue('K'.$i, ':');}
        }

		$this->excelGen->exportTo2003Bypass('Daftar Nilai');
	}
}